{% extends "base.html" %}
{% load staticfiles %}

{% block title %}操作日志{% endblock %}

{% block content %}
    <!-- Main content -->
    <body>
    <section class="content">
        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active" style="margin-top:10px"><a href="#fa-olog" onclick="tabs(1)"
                                                              data-toggle="tab">操作日志</a></li>
                <li style="margin-top:10px"><a href="#fa-rlog" onclick="tabs(2)" data-toggle="tab">策略日志</a></li>
            </ul>
            <div class="tab-content">
                <!--操作日志-->
                <div class="tab-pane active " id="fa-olog">
                    <div class="div-query">
                        <div class="lbl-query"><span>操作用户</span>
                            <input class="input-query" type="text" id="customerid">
                        </div>
                        <div class="lbl-query"><span>日志内容</span>
                            <input class="input-query" type="text" id="optinfo">
                        </div>
                        <div class="lbl-query"><span>操作类型</span>
                            <select class="input-query" id="optfunction">
                                <option value=""></option>
                                <option value="1">上传策略</option>
                                <option value="2">删除策略</option>
                                <option value="3">手工打包</option>
                                <option value="4">审批策略</option>
                                <option value="5">启动策略</option>
                                <option value="6">停止策略</option>
                                <option value="7">修改参数</option>
                                <option value="8">人工录入</option>
                                <option value="9">申请实盘</option>
                                <option value="11">移交申请</option>
                                <option value="12">移交审批</option>
                                <option value="13">一键撤单</option>
                                <option value="14">修改QB</option>
                            </select></div>
                        <div class="lbl-query">
                            <button class="btn-query" onclick="search_log()">查询
                            </button>
                        </div>
                    </div>
                    <table class="table-stc" id="logTable">
                    </table>
                </div>
                <!--策略日志-->
                <div class="tab-pane " id="fa-rlog">
                    <div class="div-query">
                        <div class="lbl-query"><span>环境</span>
                            <select class="input-query" id="tr_pl_envid">
                                <option value="1">仿真环境</option>
                                <option value="2">实盘环境</option>
                            </select>
                        </div>
                        <div class="lbl-query"><span>日期</span>
                            <input class="input-query" type="text" id="timestamp_stglog" placeholder="">
                        </div>
                        <div class="lbl-query"><span>策略ID</span>
                            <input class="input-query" type="text" id="ruleid_stglog">
                        </div>
                        <div class="lbl-query"><span>策略名称</span>
                            <input class="input-query" type="text" id="rulename_stglog">
                        </div>
                        <div class="lbl-query"><span>日志级别</span>
                            <select class="input-query" id="messagelevel_stglog">
                                <option value=""></option>
                                <option value="5">Debug</option>
                                <option value="4">Info</option>
                                <option value="3">Warning</option>
                                <option value="2">Error</option>
                                <option value="1">Critical</option>
                            </select></div>
                        <div class="lbl-query">
                            <button class="btn-query" onclick="search_stgLog()">查询
                            </button>
                        </div>
                    </div>
                    <table class="table-stc" id="RulelogTable">
                    </table>
                </div>
            </div>
        </div>
    </section>
    </body>
{% endblock %}

{% block scripts %}
    <script type="text/javascript">

        $(function () {
            search_log()
        })

        //tab页
        function tabs(n) {
            if (n == 1) {
                search_log();
                $('#fa-olog').addClass('active');
                $('#fa-rlog').removeClass('active');

            } else if (n == 2) {
                search_stgLog();
                $('#fa-olog').removeClass('active');
                $('#fa-rlog').addClass('active');

            }
        }


        //日志查询
        function search_log() {
            var customerid = $("#customerid").val();
            var optinfo = $("#optinfo").val();
            var optfunction = $("#optfunction").val();
            // 整理发送后台的数据
            var data = {
                "customerid": customerid,
                "optinfo": optinfo,
                "optfunction": optfunction
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:log' %}", {"data": JsonData, 'search_log': 1}, function (r) {
                $("#logTable").bootstrapTable("destroy").bootstrapTable({
                    height: 600,
                    columns: [
                        {
                            checkbox: true,
                            visible: false
                        },
                        {
                            field: 'sequenceno',
                            title: 'id',
                            visible: false
                        }, {
                            field: 'customerid',
                            title: '操作用户',
                            width: 100
                        }, {
                            field: 'optfunction',
                            title: '操作类型',
                            width: 100
                        }, {
                            field: 'optinfo',
                            title: '日志内容', //日志内容左对齐
                            halign: "center",
                            align: 'left' //日志内容左对齐

                        }, {
                            field: 'opttime',
                            title: '操作时间',
                            width: 200
                        }
                    ],
                    data: r,
                    // height: 500,
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    paginationHAlign: "right",
                    paginationDetailHAlign: " hidden"
                    //******前端分页设置****
                });
            });

        }

        //策略日志查询当日时间
        /**
         *获取当前时间
         *format=1精确到天
         *format=2精确到分
         */
        function getCurrentDate(format) {
            var now = new Date();
            var year = now.getFullYear(); //得到年份
            var month = now.getMonth();//得到月份
            var date = now.getDate();//得到日期
            var day = now.getDay();//得到周几
            var hour = now.getHours();//得到小时
            var minu = now.getMinutes();//得到分钟
            var sec = now.getSeconds();//得到秒
            month = month + 1;
            if (month < 10) month = "0" + month;
            if (date < 10) date = "0" + date;
            if (hour < 10) hour = "0" + hour;
            if (minu < 10) minu = "0" + minu;
            if (sec < 10) sec = "0" + sec;
            var time = "";
            //精确到天
            if (format == 1) {
                time = year + "-" + month + "-" + date;
            }
            //精确到分
            else if (format == 2) {
                time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
            }
            return time;
        }

        today_ = getCurrentDate(1)

        $("#timestamp_stglog").val(today_)
        $("#timestamp_stglog").datetimepicker({
            format: 'yyyy-mm-dd',
            startView: 'year',
            maxView: 'year',
            minView: 'month',
            autoclose: true,
            pickerPosition: "bottom-right",
            language: 'zh',
            bootcssVer: 3, //显示箭头
        });

        //策略日志查询
        function search_stgLog() {
            var envir_stglog = $("#tr_pl_envid").val();
            var timestamp_stglog = $("#timestamp_stglog").val();
            var ruleid_stglog = $("#ruleid_stglog").val();
            var rulename_stglog = $("#rulename_stglog").val();
            var messagelevel_stglog = $("#messagelevel_stglog").val();
            // 整理发送后台的数据
            var data = {
                "envir_stglog": envir_stglog,
                "timestamp_stglog": timestamp_stglog,
                "ruleid_stglog": ruleid_stglog,
                "rulename_stglog": rulename_stglog,
                "messagelevel_stglog": messagelevel_stglog,
            };

            var JsonData = JSON.stringify(data);
            $.post("{% url 'trade:log' %}", {"data": JsonData, 'search_stglog': 1}, function (r) {
                $("#RulelogTable").bootstrapTable("destroy").bootstrapTable({
                    exportTypes: ['excel'],
                    showExport: true,
                    exportDataType: "all",
                    exportOptions: {
                        fileName: "stg-log"
                    },
                    height: 600,
                    columns: [
                        {
                            field: 'timestamp',
                            title: '日志时间',
                            width: 100
                        }, {
                            field: 'ruleid',
                            title: '策略ID',
                            width: 100
                        }, {
                            field: 'rulename',
                            title: '策略名称',
                            width: 200
                        }, {
                            field: 'messagelevel',
                            title: '日志级别',
                            width: 100
                        }, {
                            field: 'message',
                            title: '日志内容',
                            halign: "center",
                            align: 'left'
                        }
                    ],
                    data: r,
                    // height: 500,
                    //******前端分页设置****
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    paginationHAlign: "right",
                    paginationDetailHAlign: " hidden"
                    //******前端分页设置****
                });

            });
        }


    </script>
{% endblock %}